<template>
	<view class="content">
		<button class="avatar" open-type="chooseAvatar" @chooseavatar="getAvatar">
			<text>个人头像</text>
			<image class="img" :src="data.avatar" mode=""></image>
			<image class="icon" src="../../static/right.png" mode=""></image>
		</button>

		<view class="view">
			<view class="view1">
				<text>账号</text>
				<input type="text" :disabled="true" v-model="data.account" placeholder="请输入账号" />
			</view>
			<view class="view1">
				<text>密码</text>
				<input type="text" v-model="data.oldPassword" placeholder="请输入原密码" :password="true" />
			</view>
			<view class="view1">
				<text>修改密码</text>
				<input type="text" v-model="data.newPassword" placeholder="请输入要修改的密码" :password="true" />
			</view>
			<view class="view1">
				<text>确认密码</text>
				<input type="text" v-model="data.newPassword1" placeholder="请再次确认要修改的密码" :password="true" />
			</view>
		</view>
		<view class="bottom">
			<view class="button" @click="submit()">
				提交保存
			</view>
		</view>
	</view>
</template>

<script>
	import {
		showToast
	} from "@/utils/media.js"
	import auth from "@/api/auth";

	export default {
		data() {
			return {
				user_info: {
					avatar: ''
				},
				data: {
					account: '',
					oldPassword: '',
					newPassword: '',
					newPassword1: ''
				}
			};
		},
		onLoad() {
			this.userInfo()
		},
		methods: {
			getAvatar(e) {
				this.data.avatar = e.detail.avatarUrl
				auth.updateAvatar(e.detail.avatarUrl).then(data => {
					this.data.avatar = data.link
					
					auth.userUpdate(this.data).then(res => {
						if (res.code !== 200) {
							showToast({
								title: res.msg
							})
							return
						}
						showToast({
							title: res.msg
						})
						this.userInfo()
					})
				})
			},
			userInfo() {
				auth.userInfo().then(res => {
					if (res.code !== 200) {
						return
					}
					this.data =res.data
					uni.setStorageSync('userInfo', res.data)
				})
			},
			submit() {
				let {
					account,
					oldPassword,
					newPassword,
					newPassword1
				} = this.data
				if (account == '') {
					showToast({
						title: '账号不能为空'
					})
					return
				}
				if (oldPassword == '') {
					showToast({
						title: '密码不能为空'
					})
					return
				}
				if (newPassword == '') {
					showToast({
						title: '新的密码不能为空'
					})
					return
				}
				if (newPassword1 == '') {
					showToast({
						title: '确认密码不能为空'
					})
					return
				}
				if (newPassword != newPassword1) {
					showToast({
						title: '两次新密码不一致'
					})
					return
				}
				auth.updatePassword(this.data).then(res => {
					if (res.code != 200) {
						showToast({
							title: res.msg
						})
						return
					}
					showToast({
						title: res.msg
					})
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/mine/mine'
						})
					}, 800);
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;

		.avatar {
			margin-top: 20rpx;
			width: 686rpx;
			height: 140rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(204, 204, 204, 0.14);
			border-radius: 10rpx;
			display: flex;
			align-items: center;

			>text {
				margin-left: 20rpx;
				font-weight: 600;
				font-size: 28rpx;
				color: #1A1A18;
			}

			.img {
				margin-left: auto;
				width: 76rpx;
				height: 76rpx;
				border: 1px solid;
				border-radius: 50%;
			}

			.icon {
				margin-left: 20rpx;
				margin-right: 20rpx;
				width: 14rpx;
				height: 24rpx;
			}
		}

		.view {
			margin-top: 20rpx;
			width: 686rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 3rpx 16rpx 1rpx rgba(204, 204, 204, 0.26);
			border-radius: 10rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.view1 {
				width: 622rpx;
				height: 104rpx;
				display: flex;
				align-items: center;
				border-bottom: 1px solid #EEE;

				>text {
					margin-left: 20rpx;
					font-weight: 600;
					font-size: 28rpx;
					color: #1A1A18;
				}

				>input {
					margin-left: auto;
					text-align: right;
				}
			}
		}

		.bottom {
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 160rpx;
			background-color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;

			.button {
				width: 686rpx;
				height: 92rpx;
        background: $custom-btn-color;
				border-radius: 46rpx;
				line-height: 92rpx;
				text-align: center;
				font-weight: 600;
				font-size: 32rpx;
				color: #FFFFFF;
			}
		}
	}
</style>